const dayjs = require("./dayjs");

let startX = 0
let moveX = 0
let moveDistanc = 0

const move = 80

Component({
	options: {
		"styleIsolation": "apply-shared"
	},
	/**
	 * 组件的属性列表
	 */
	properties: {

	},

	/**
	 * 组件的初始数据
	 */
	data: {
		week: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
		data: [],
		toDay: dayjs().format('YYYY-MM-DD'),
		year: dayjs().year(),
		month: dayjs().month() + 1,
		yearMonth: '',
		translateX: 'translateX(0)',
		transition: ''
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		// 今天
		toDay(){
			this.setData({
				year: dayjs().year(),
				month: dayjs().month() + 1,
				yearMonth: dayjs().format('YYYY-MM')
			})
			this.threeMonthDays(this.data.yearMonth)
		},
		// 手指触摸
		handleTouchStart(event){
			startX = event.touches[0].clientX
			this.setData({
				transition: ''
			})
		},
		// 手指滑动
		handleTouchMove(event){
			moveX = event.touches[0].clientX
			moveDistanc = moveX - startX
			if(moveDistanc > move){
				moveDistanc = move
			}
			if(moveDistanc < -move){
				moveDistanc = -move
			}
			this.setData({
				translateX: `translateX(${moveDistanc}px)`
			})
		},
		// 手指离开
		handleTouchEnd(){
			const changeYearMonth = () => {
				const that = this
				setTimeout(() => {
					that.setData({
						transition: ''
					})
					that.threeMonthDays(that.data.yearMonth)
					that.setData({
						translateX: `translateX(0)`,
						year: that.data.yearMonth.slice(0, 4),
						month: that.data.yearMonth.slice(5, 7)
					})
				}, 350)
			}
			if(moveDistanc < move && moveDistanc > -move){
				this.setData({
					translateX: `translateX(0)`,
					transition: 'transform 0.3s ease-in-out'
				})
			} else if(moveDistanc >= move){
				this.setData({
					translateX: `translateX(750rpx)`,
					transition: 'transform 0.3s ease-in-out',
					yearMonth: this.lastYearMonth(this.data.year, this.data.month),
				})
				changeYearMonth()
			} else {
				this.setData({
					translateX: `translateX(-750rpx)`,
					transition: 'transform 0.3s ease-in-out',
					yearMonth: this.nextYearMonth(this.data.year, this.data.month),
				})
				changeYearMonth()
			}
			moveDistanc = 0
		},
		// 生成3个月数据
		threeMonthDays(yearMonth){
			const month = parseInt(yearMonth.slice(5, 7))
			const year = parseInt(yearMonth.slice(0, 4))
			const arr0 = this.getDay(this.lastYearMonth(year, month))
			const arr1 = this.getDay(yearMonth)
			const arr2 = this.getDay(this.nextYearMonth(year, month))
			this.setData({
				data:[arr0, arr1, arr2]
			})
		},
		// 上一月
		lastYearMonth(year, month){
			year = parseInt(year)
			month = parseInt(month)
			if(month - 1 < 1){
				return (year - 1) + '-' + 12
			} else {
				if(month - 1 < 10){
					return year + '-0' + (month - 1)
				} else {
					return year + '-' + (month - 1)
				}
			}
		},
		// 下一个月
		nextYearMonth(year, month){
			year = parseInt(year)
			month = parseInt(month)
			if(month + 1 > 12){
				return (year + 1) + '-01'
			} else {
				if(month + 1 < 10){
					return year + '-0' + (month + 1)
				} else {
					return year + '-' + (month + 1)
				}
			}
		},
		// 生成日历数组
		getDay(yearMonth){
			const month = parseInt(yearMonth.slice(5, 7))
			const year = parseInt(yearMonth.slice(0, 4))
			let arr = []
			let str = ''
			// 获取上个月多余数组
			const firstMonthWeek = dayjs(yearMonth + '-01').day()
			let lastMonthDays = 0
			
			if(firstMonthWeek === 1){
				lastMonthDays = 0
			} else {
				lastMonthDays = firstMonthWeek - 1
				for(let i = lastMonthDays ; i > 0 ; i--){
					str = this.lastYearMonth(year, month) + '-' + (dayjs(this.lastYearMonth(year, month) + '01').daysInMonth() - i + 1)
					arr.push({date: str, day: str.slice(8, 10), gray: true})
				}
			}			
			// 获取本月数组
			for(let i = 1 ; i <= dayjs(yearMonth).daysInMonth() ; i++){
				if(i < 10){
					str = yearMonth + '-0' + i
				} else {
					str = yearMonth + '-' + i
				}
				arr.push({date: str, day: str.slice(8, 10)})
			}
			// 获取下个月多余数组
			const endMonthWeek = dayjs(yearMonth + '-' +dayjs(yearMonth).daysInMonth()).day()
			if(endMonthWeek != 7){
				let nextMonthDays = 7 - endMonthWeek
				for(let i = 0 ; i < nextMonthDays ; i++){
					str = this.nextYearMonth(year, month) + '-0' + (i + 1)
					arr.push({date: str, day: str.slice(8, 10), gray: true})
				}
			}
			let newArr = []
			for(let i = 1; i <= arr.length / 7 ; i++){
				newArr[i-1] = []
				for(let j = 1; j <= arr.length ; j++){
					if(j <= i * 7 && j > (i - 1) * 7){
						newArr[i-1].push(arr[j-1])
					}
				}
			}
			return newArr
		}
	},
	ready(){
		const yearMonth = dayjs().format('YYYY-MM-DD').slice(0, 7)
		this.setData({
			yearMonth
		})
		this.threeMonthDays(this.data.yearMonth)
	}
})
